package com.yzt.p_base.utils;

import android.annotation.SuppressLint;
import android.content.Context;

import com.yzt.p_base.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * author : bob
 * date   : 2022/11/30
 * desc   :
 */
public class DateUtils {

    public static final String FORMAT = "yyyy-MM-dd";

    @SuppressLint("ConstantLocale")
    private static final DateFormat DEFAULT_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());

    /**
     * 获取系统最新时间
     */
    public static long getNowTimeStamp() {
        Date date = new Date();
        return date.getTime();
    }

    /**
     * 时间戳转成时间格式
     *
     * @param t       时间戳
     * @param pattern 格式
     * @return 时间
     */
    public static String formatTime(long t, String pattern) {
        SimpleDateFormat sdf = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdf.format(new Date(t));
    }

    /**
     * 获取年月日
     */
    public static String getDateTime(Date date, String type) {
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat format = new SimpleDateFormat(type);
        return format.format(date);
    }

    public static int getAge(Date birthDay, Date currentTime) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentTime);
        int yearNow = cal.get(Calendar.YEAR);  //当前年份
        int monthNow = cal.get(Calendar.MONTH);  //当前月份
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期

        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;   //计算整岁数
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;//当前日期在生日之前，年龄减一
                }
            } else {
                age--;//当前月份在生日之前，年龄减一
            }
        }
        return age;
    }

    /**
     * 获取年月日
     */
    public static int[] getYearMonthDay(Date date) {
        int[] datetime = new int[3];
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        datetime[0] = calendar.get(Calendar.YEAR);
        datetime[1] = calendar.get(Calendar.MONTH);
        datetime[2] = calendar.get(Calendar.DAY_OF_MONTH);
        return datetime;
    }

    public static String getHoroscope(Date date, Context context) {
        int[] yearMonthDay = getYearMonthDay(date);
        int month = yearMonthDay[1] + 1;
        int day = yearMonthDay[2];
        String[] starArr = {context.getString(R.string.capricorn),
                context.getString(R.string.aquarius),
                context.getString(R.string.pisces),
                context.getString(R.string.aries),
                context.getString(R.string.taurus),
                context.getString(R.string.gemini),
                context.getString(R.string.cancer),
                context.getString(R.string.leo),
                context.getString(R.string.virgo),
                context.getString(R.string.libra),
                context.getString(R.string.scorpio),
                context.getString(R.string.sagittarius)};
        int[] dayArr = {22, 20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22};  // 两个星座分割日
        int index = month;
        // 所查询日期在分割日之前，索引-1，否则不变
        if (day < dayArr[month - 1]) {
            index = index - 1;
        }
        if (index > starArr.length - 1) {
            index = 0;
        }
        // 返回索引指向的星座string
        return starArr[index];
    }

    /**
     * 获取当前时间字符串
     * <p>格式为format</p>
     *
     * @param format 时间格式
     * @return 时间字符串
     */
    public static String getNowString(final DateFormat format) {
        return millis2String(System.currentTimeMillis(), format);
    }

    /**
     * 将时间戳转为时间字符串
     * <p>格式为format</p>
     *
     * @param millis 毫秒时间戳
     * @param format 时间格式
     * @return 时间字符串
     */
    public static String millis2String(final long millis, final DateFormat format) {
        return format.format(new Date(millis));
    }

    /**
     * 将时间戳转为时间字符串
     * <p>格式为format</p>
     *
     * @param millis 毫秒时间戳
     * @return 时间字符串
     */
    public static String millis2String(final long millis) {
        return millis2String(millis, DEFAULT_FORMAT);
    }


    public static String formatTime(int time) {
        int h = time / 3600;
        int m = (time % 3600) / 60;
        int s = time % 60;
        return getStringFormat(h) + ":" + getStringFormat(m) + ":" + getStringFormat(s);
    }

    private static String getStringFormat(int in) {
        String sin;
        if (in < 10) {
            sin = "0" + in;
        } else {
            sin = String.valueOf(in);
        }
        return sin;
    }


    public static String getNowFormatDay() {
        return DateUtils.getDateTime(new Date(), "yyyy-MM-dd");
    }
}

